# @Author ZhangGJ
# @Date 2021/12/01 10:19

import cv2.cv2 as cv2
import numpy as np


# 腐蚀
def erode():
    img = cv2.imread('../images/cactus.jpg')
    k = np.ones((3, 3), np.uint8)
    cv2.imshow('img', img)
    dst = cv2.erode(img, k)
    cv2.imshow('dst', dst)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 膨胀
def dilate():
    img = cv2.imread('../images/sunset.jpg')
    k = np.ones((9, 9), np.uint8)
    cv2.imshow('img', img)
    dst = cv2.dilate(img, k)
    cv2.imshow('dst', dst)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 开运算
def open_operation():
    img = cv2.imread('../images/nigella.png')
    k = np.ones((5, 5), np.uint8)
    cv2.imshow('img', img)
    dst = cv2.erode(img, k)
    dst = cv2.dilate(dst, k)
    cv2.imshow('dst', dst)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 闭运算
def close_operation():
    img = cv2.imread('../images/tianye.png')
    k = np.ones((15, 15), np.uint8)
    cv2.imshow('img', img)
    dst = cv2.dilate(img, k)
    dst = cv2.erode(dst, k)
    cv2.imshow('dst2', dst)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 梯度运算
def gradient_operation():
    img = cv2.imread('../images/spider.png')
    k = np.ones((5, 5), np.uint8)
    cv2.imshow('img', img)
    dst = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, k)
    cv2.imshow('dst', dst)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 顶帽运算
def top_hat_operation():
    img = cv2.imread('../images/spider.png')
    k = np.ones((5, 5), np.uint8)
    cv2.imshow('img', img)
    dst = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, k)
    cv2.imshow('dst', dst)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 黑帽运算
def black_hat_operation():
    img = cv2.imread('../images/spider2.png')
    k = np.ones((5, 5), np.uint8)
    cv2.imshow('img', img)
    dst = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, k)
    cv2.imshow('dst', dst)
    cv2.waitKey()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    black_hat_operation()
